package com.tsl.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.tsl.reggie.Exception.CustomException;
import com.tsl.reggie.common.R;
import com.tsl.reggie.domain.po.Category;
import com.tsl.reggie.domain.po.Dish;
import com.tsl.reggie.domain.po.Setmeal;
import com.tsl.reggie.mapper.CategoryMapper;
import com.tsl.reggie.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsl.reggie.service.IDishService;
import com.tsl.reggie.service.ISetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author 唐书亮
 * @since 2024-01-20
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    @Autowired
    IDishService dishService;

    @Autowired
    ISetmealService setmealService;
    @Override
    public Boolean remove(Long ids) {
        //判断分类是否绑定了菜品
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId, ids);
        List<Dish> dish = dishService.list(queryWrapper);
        if (ObjectUtils.isNotEmpty(dish)) {
            throw new CustomException("此分类已绑定菜品,无法删除");
        }
        LambdaQueryWrapper<Setmeal> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Setmeal::getCategoryId, ids);
        Setmeal setmeal = setmealService.getOne(queryWrapper1);
        //判断分类是否绑定了套餐
        if (ObjectUtils.isNotEmpty(setmeal)) {
            throw new CustomException("此分类已绑定套餐,无法删除");
        }

        //删除分类

        return removeById(ids);
    }
}
